package com.wm.dao;

import com.wm.entity.RbacRole;
import com.wm.util.BeanUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper

public interface RoleDao {

    @Insert("insert into rbac_role(name,description,status) values(#{name},#{description},#{status}) ")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void insert(RbacRole rbacRole);


    @Select("select * from rbac_role")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "description", column = "description"),
            @Result(property = "status", column = "status"),
            @Result(property = "perms",column = "id",many =@Many(select = "com.wm.dao.PermDao.getByRoleId",fetchType = FetchType.LAZY))

    })
    List<RbacRole> getAll(RbacRole role);
    List<RbacRole> getAll();


    @Select("select * from rbac_role where id =#{id}")
    RbacRole selectId (Integer id);

    @Delete("delete from rbac_role where id =#{id}")
    void deleteId(Integer id);

   // @Update("update rbac_role set name=#{name},description=#{description},status=#{status} where id=#{id}")
    @UpdateProvider(type = SqlProvider.class,method = "update")
    void UpdateRole(RbacRole role);

    @Select("select count(*) from rbac_role where name=#{name}")
    int getNumOfName(String name);

    @Select("select * from rbac_role where id=#{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "description", column = "description"),
            @Result(property = "status", column = "status"),
            @Result(property = "perms",column = "id",many =@Many(select = "com.wm.dao.PermDao.getByRoleId",fetchType = FetchType.LAZY))

    })
    public RbacRole getById(int id);

    @Select("select * from rbac_role where name=#{name}")
     RbacRole getByName(String name);


    public static class SqlProvider {
        public static String update(RbacRole role) {

            SQL sql = new SQL();
            sql.UPDATE("rbac_role");
            if (BeanUtils.isNotEmpty(role.getName())) {
                if (BeanUtils.isNotEmpty(role.getName())) {
                    sql.SET("name=#{name}");
                }
                if (BeanUtils.isNotEmpty(role.getDescription())) {
                    sql.SET("description=#{description}");
                }
                if (BeanUtils.isNotEmpty(role.getStatus())) {
                    sql.SET("status=#{status}");
                }
                sql.WHERE("id=#{id}");
            }
            return sql.toString();
//           return new SQL() {
//                {
//                    UPDATE("rbac_role");
//                    if (BeanUtils.isEmpty(role.getName())) {
//                        SET("name=#{name}");
//                    }
//                    if (BeanUtils.isEmpty(role.getDescription())) {
//                        SET("Description=#{Description}");
//                    }
//                    if (BeanUtils.isEmpty(role.getStatus())) {
//                        SET("Status=#{Status}");
//                    }
//                    WHERE("id=#{id}");
//
//                }
//            }.toString();

        }
    }
}
